//
//  FeedBackViewController.m
//  YingKeEducation
//
//  Created by 戴伟 on 2019/1/26.
//  Copyright © 2019 戴伟. All rights reserved.
//

#import "FeedBackViewController.h"
#import "UITextViewPlaceholder.h"

#define MAX_LIMIT_NUMS 200

@interface FeedBackViewController ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITextViewPlaceholder *textView;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet UITextField *contactTextField;


@property (weak, nonatomic) IBOutlet UILabel *numberLabel;//剩余字数

@property (weak, nonatomic) IBOutlet UIButton *typeBtn1;
@property (weak, nonatomic) IBOutlet UIButton *typeBtn2;
@property (weak, nonatomic) IBOutlet UIButton *typeBtn3;

@property (nonatomic, assign) NSInteger type;

@end

@implementation FeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"意见反馈";
    
    _type = 1;
    
    _containerView.layer.shadowOffset = CGSizeMake(0, 0);
    _containerView.layer.shadowColor = GRAYTEXTCOLOR.CGColor;
    _containerView.layer.shadowOpacity = 0.5;
    
    _textView.placeholder = @"请输入您要反馈的意见（最多200字）...";
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.delegate = self;
    _textView.placeholderTextColor = [UIColor colorWithHexString:@"#B7BCC6" andAlpha:1];
    _textView.font = [UIFont systemFontOfSize:14];
    
    _submitBtn.layer.cornerRadius = 22.5;
    _submitBtn.layer.shadowOffset = CGSizeMake(2, 2);
    _submitBtn.layer.shadowColor = THEMECOLOR.CGColor;
    _submitBtn.layer.shadowOpacity = 0.4;
}

-(void)textViewDidChange:(UITextView *)textView{
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    
    //如果在变化中是高亮部分在变，就不要计算字符了
    if (selectedRange && pos) {
        return;
    }
    
    NSString  *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;
    
    if (existTextNum > MAX_LIMIT_NUMS)
    {
        //截取到最大位置的字符
        NSString *s = [nsTextContent substringToIndex:MAX_LIMIT_NUMS];
        
        [textView setText:s];
    }
    //不让显示负数
    self.numberLabel.text = [NSString stringWithFormat:@"%ld/%d",existTextNum, MAX_LIMIT_NUMS];
    if (existTextNum > 0) {
        self.numberLabel.textColor = THEMECOLOR;
    }else{
        self.numberLabel.textColor = [UIColor colorWithHexString:@"#D8DCE6" andAlpha:1];
    }
}

- (IBAction)submit:(id)sender {
    [self.view endEditing:YES];
    if (_textView.text.length == 0) {
        [MBProgressHUD showWarning:@"请输入您的反馈" toView:nil];
        return;
    }
    
    [MBProgressHUD showMessag:@"" toView:self.view];
    NSDictionary *dict = @{
                           @"content": _textView.text,
                           @"contactInfo": _contactTextField.text
                           };
//    [NetWork sendPostUrl:@"person/feedback" withParams:dict success:^(id responseBody) {
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//        [MBProgressHUD showSuccess:@"提交成功" toView:nil];
//        [self.navigationController popViewControllerAnimated:YES];
//    } failure:^(NSError *error) {
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
//    }];
}

- (IBAction)btn1Clicked:(id)sender {
    _typeBtn1.selected = YES;
    _typeBtn2.selected = NO;
    _typeBtn3.selected = NO;
    _type = 1;
}

- (IBAction)btn2Clicked:(id)sender {
    _typeBtn1.selected = NO;
    _typeBtn2.selected = YES;
    _typeBtn3.selected = NO;
    _type = 2;
}

- (IBAction)btn3Clicked:(id)sender {
    _typeBtn1.selected = NO;
    _typeBtn2.selected = NO;
    _typeBtn3.selected = YES;
    _type = 3;
}

- (IBAction)call:(id)sender {
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"025-86772786"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

@end
